package com.tongzhou.tongzhou.quot.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IPaperDAO;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.SearchPaperCondition;
import com.tongzhou.util.Util;

public class PaperDAO implements IPaperDAO{

	@Override
	public int addPaper(Paper paper) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into paper(paper_name,price,keng,paper_type_id) ");
		sql.append("values('"+Util.changeToObj(paper.getPaper_name())+"',");
		sql.append("'"+paper.getPrice()+"',");
		sql.append("'"+paper.getKeng()+"',");
		sql.append("'"+paper.getPaper_type_id()+"')");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deletePaper(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from paper where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public Paper getPaperById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,paper_name,price,brand,params,keng,paper_type_id ");
		sql.append(" from paper ");
		sql.append(" where id='"+id+"'");
		Paper paper=DBUtil.query(sql.toString(), new BeanHandler<Paper>(Paper.class));
		return paper;
	}

	@Override
	public List<Paper> listPaper(SearchPaperCondition condition)
			throws DBException {
		List<Paper> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		if(condition.getKeng() != null && !condition.getKeng().equals("")){
			sqlCondition.append(" and keng =?");
			paramList.add(condition.getKeng());
		}
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from paper ");
		sql.append(" where 1=1 ");
		sql.append(" and paper_type_id='"+condition.getPaper_type_id()+"'");
		sql.append(sqlCondition);
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,paper_name,price,brand,params,keng,paper_type_id ");
		sql.append(" from paper ");
		sql.append(" where 1=1 ");
		sql.append(" and paper_type_id='"+condition.getPaper_type_id()+"'");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<Paper>(Paper.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updatePaper(Paper paper) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update paper set ");
		sql.append("paper_name='"+Util.changeToObj(paper.getPaper_name())+"',");
		sql.append("price='"+paper.getPrice()+"',");
		sql.append("brand='"+Util.changeToObj(paper.getBrand())+"',");
		sql.append("params='"+Util.changeToObj(paper.getParams())+"',");
		sql.append("keng='"+Util.changeToObj(paper.getKeng())+"' ");
		sql.append("where id='"+paper.getId()+"'");
		DBUtil.update(sql.toString());
	}
	
	@Override
	public List<Paper> getOnePaperList(int paper_type_id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.paper_name,p.price,p.brand,p.params,p.keng,p.paper_type_id ");
		sql.append(" from paper p ");
		sql.append(" where 1=1 and p.keng=1 ");
		sql.append(" and p.paper_type_id="+paper_type_id+" ");
		List<Paper> result=DBUtil.query(sql.toString(),new BeanListHandler<Paper>(Paper.class));
		return result;
	}
	
	@Override
	public List<Paper> getTwoPaperList(int paper_type_id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.paper_name,p.price,p.brand,p.params,p.keng,p.paper_type_id ");
		sql.append(" from paper p ");
		sql.append(" where 1=1 and p.keng=2 ");
		sql.append(" and p.paper_type_id="+paper_type_id+" ");
		List<Paper> result=DBUtil.query(sql.toString(),new BeanListHandler<Paper>(Paper.class));
		return result;
	}

}
